package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.StageStyle;

import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML private ComboBox<String> destinationPort;
    @FXML private ComboBox<String> mineSamplingTypeInput;
    @FXML private ComboBox<String> portSamplingTypeInput;
    @FXML private ComboBox<String> typeChange;
    @FXML private TabPane tabs;

    @FXML private TableView table1;
    @FXML private TableColumn coalType, seqNum, trainNum, date, time, firstTrain, lastTrain, mineMt, portMt, minusMt, mineMad, portMad, minusMad,
            mineAd, portAd, minusAd, mineVd, portVd, minusVd, mineStd, portStd, minusStd, mineQnetar, portQnetar, minusQnetar, mineSamplingType, portSamplingType, port, testOrg, deliveryType;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        setData();
        ObservableList<CoalStatistic> data = FXCollections.observableArrayList();
        data.addAll(getDummyData(), getDummyData(), getDummyData(), getDummyData(), getDummyData());
        coalType.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String>("type"));
        seqNum.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String>("seqNum"));
        trainNum.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String >("trainNum"));
        date.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String>("date"));
        time.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String >("time"));
        firstTrain.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Integer>("firstTrain"));
        lastTrain.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Integer>("lastTrain"));
        mineMt.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("mineMt"));
        portMt.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("portMt"));
        minusMt.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("minusMt"));
        mineMad.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("mineMad"));
        portMad.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("portMad"));
        minusMad.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("minusMad"));
        mineAd.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("mineAd"));
        portAd.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("portAd"));
        minusAd.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("minusAd"));
        mineVd.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("mineVd"));
        portVd.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("portVd"));
        minusVd.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("minusVd"));
        mineStd.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("mineStd"));
        portStd.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("portStd"));
        minusStd.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("minusStd"));
        mineQnetar.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("mineQnetar"));
        portQnetar.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("portQnetar"));
        minusQnetar.setCellValueFactory(new PropertyValueFactory<CoalStatistic, Double>("minusQnetar"));
        mineSamplingType.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String>("mineSamplingType"));
        portSamplingType.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String>("portSamplingType"));
        port.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String>("port"));
        testOrg.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String>("testOrg"));
        deliveryType.setCellValueFactory(new PropertyValueFactory<CoalStatistic, String>("deliveryType"));
        table1.setItems(data);
    }

    public void setData() {
        destinationPort.getItems().addAll(DataHelper.toArray(DataHelper.Port.class));
        mineSamplingTypeInput.getItems().addAll(DataHelper.toArray(DataHelper.SamplingType.class));
        portSamplingTypeInput.getItems().addAll(DataHelper.toArray(DataHelper.SamplingType.class));
        typeChange.getItems().addAll(DataHelper.toArray(DataHelper.CoalType.class));
//        testOrg.getItems().addAll("秦皇岛市检","秦皇岛商检","京唐港市检","京唐港商检","曹妃甸商检","秦华煤检","华夏力鸿");
    }

    @FXML
    public void newTab() throws IOException {
        if (tabs.getSelectionModel().getSelectedItem().getText().equals("+")) {
            TextInputDialog dialog = new TextInputDialog("新增矿名称");
            dialog.initStyle(StageStyle.UTILITY);
            Optional<String> result = dialog.showAndWait();
            if (result.isPresent()) {
                createTab(result.get());
            } else {
                tabs.getSelectionModel().selectPrevious();
            }
        }
    }

    private void createTab(String name) throws IOException {
        int length = tabs.getTabs().size();
        Tab newTab = FXMLLoader.load(this.getClass().getResource("template.fxml"));
        tabs.getTabs().add(length - 1, newTab);
        tabs.getSelectionModel().select(length - 1);
        tabs.getSelectionModel().getSelectedItem().setText(name);
    }

    private CoalStatistic getDummyData() {
        return new CoalStatistic("平九", "P1-19", "872011", "2017-01-01","23:30",4378545, 4388442, 9.4, 9.7, 9.4, 9.7, 9.4, 9.7, 9.4, 9.7, 9.4, 9.7, 9.4, 9.7,"机采","手工","秦港","商检","");
    }

}
